Python Httplib2
πΈ The Hitchhiker's Guide to httplib2
in Python ππβ
Welcome, fellow Earthling developer, to the whimsical world of httplib2
β your trusty HTTP sidekick in Python. Whether you're shaking hands with servers, poking around web pages, or charming APIs with POST requests, httplib2
is here to help (with fewer explosions than intergalactic travel)!
π¦ A Quick Nginx Status Checkβ
Before we dive in, letβs make sure our local web spaceship (aka nginx
) is up and flying:
$ service nginx status
* nginx is running
Awesome! Nginx is alive. Some of our demos will talk to PHP scripts running on this local server. It's like chatting with your neighbor through tin cans (but techier).
π Table of Awesomenessβ
- Check
httplib2
Library Version - Read a Web Page like a Pro
- Fire a HEAD Request π«
- GET Some Data
- POST Like a Rockstar
- Customize User Agent (Tell 'em who you are!)
- Credentials? We've got you covered.
1. π΅οΈ Check httplib2
Version β AKA, Know Thy Toolsβ
#!/usr/bin/python3
import httplib2
print(httplib2.__version__)
print(httplib2.__copyright__)
print(httplib2.__doc__)
π¨οΈ Output:
0.8
Copyright 2006, Joe Gregorio
httplib2
A caching http interface that supports ETags and gzip
to conserve bandwidth.
So clean. So meta. So⦠HTTP.
2. π° Read a Web Page (Like It's 1999)β
#!/usr/bin/python3
import httplib2
http = httplib2.Http()
content = http.request("http://www.something.com")[1]
print(content.decode())
π¨οΈ Output:
<html><head><title>Something.</title></head>
<body>Something.</body>
</html>
Yes, the HTML is basic, but it works! Baby steps, young padawan.
3. βοΈ Strip Those HTML Tagsβ
#!/usr/bin/python3
import httplib2
import re
http = httplib2.Http()
content = http.request("http://www.something.com")[1]
stripped = re.sub('<[^<]+?>', '', content.decode())
print(stripped)
π¨οΈ Output:
Something.
Something.
No tags, no worries. This is the minimalist lifestyle your content dreams of.
4. β Check HTTP Status Like a Bouncer at a Nightclubβ
#!/usr/bin/python3
import httplib2
http = httplib2.Http()
resp = http.request("http://www.something.com")[0]
print(resp.status)
resp = http.request("http://www.something.com/news/")[0]
print(resp.status)
π¨οΈ Output:
200
404
One's good, oneβs bad. 200 means βAll good here,β 404 means βThat page left the party.β
5. π§ Use HEAD Method β Just the Headers, Pleaseβ
#!/usr/bin/python3
import httplib2
http = httplib2.Http()
resp = http.request("http://www.something.com", "HEAD")[0]
print("Server: " + resp['server'])
print("Last modified: " + resp['last-modified'])
print("Content type: " + resp['content-type'])
print("Content length: " + resp['content-length'])
π¨οΈ Output:
Server: Apache/2.4.12
Last modified: Mon, 25 Oct 1999
Content type: text/html
Content length: 72
Itβs like checking a bookβs back cover before reading it. Smart move.
6. π§ GET That Data (Literally)β
greet.php
on server:
<?php
echo "Hello " . htmlspecialchars($_GET['name']);
?>
Python GET request:
#!/usr/bin/python3
import httplib2
http = httplib2.Http()
content = http.request("http://localhost/greet.php?name=Peter", method="GET")[1]
print(content.decode())
π¨οΈ Output:
Hello Peter
β This is how you charm your server with a GET. Smooth!
7. π¦ POST Like a Bossβ
target.php
:
<?php
echo "Hello " . htmlspecialchars($_POST['name']);
?>
Python POST request:
#!/usr/bin/python3
import httplib2
import urllib
http = httplib2.Http()
body = {'name': 'Peter'}
content = http.request("http://localhost/target.php",
method="POST",
headers={'Content-type': 'application/x-www-form-urlencoded'},
body=urllib.parse.urlencode(body))[1]
print(content.decode())
π¨οΈ Output:
Hello Peter
π POST β for when GET just wonβt cut it.
8. π΅οΈββοΈ Who Are You? Set the User Agentβ
agent.php
:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
Python script:
#!/usr/bin/python3
import httplib2
http = httplib2.Http()
content = http.request("http://localhost/agent.php", method="GET",
headers={'user-agent': 'Python script'})[1]
print(content.decode())
π¨οΈ Output:
Python script
πΆοΈ Identify yourself with pride!
9. π Basic Authentication β Password, Pleaseβ
Step 1: Create a userβ
sudo htpasswd -c /etc/nginx/.htpasswd user7
Step 2: Configure nginxβ
location /secure {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Step 3: Python script with credsβ
#!/usr/bin/python3
import httplib2
user = 'user7'
passwd = '7user'
http = httplib2.Http()
http.add_credentials(user, passwd)
content = http.request("http://localhost/secure/")[1]
print(content.decode())
π¨οΈ Output:
<p>
This is a secure page.
</p>
β Access granted! You may now view the secret scrolls (or pages).
π Wrapping Upβ
Weβve learned how to:
- Whisper sweet GETs to a server
- Sling headers like a boss
- Post data like a caffeinated intern
- Pass through security with credentials
All thanks to the mighty httplib2
.
Crafted with bytes & love by Jan Bodnar (via ZetCode.com) and sprinkled with fun by ChatGPT π€β¨
Now go forth and HTTP like a hero! π